package flychess.view;

import flychess.data.GameMap;
import flychess.entity.Player;
import flychess.util.XMLParser;
import java.util.HashMap;
import java.util.Map;

public abstract class DisplayMap {
    protected GameMap gMap;
    protected static final Map<String, String> icons = new HashMap<>();
    protected static Map<Integer, String> indexToIconName = new HashMap<>();
    public static Map<String, Integer> iconNameToIndex = new HashMap<>();
    static {
        XMLParser.readIconList(icons, indexToIconName, iconNameToIndex);
    }
    /**
     * 抽象方法，显示地图
     * @param player1 玩家1
     * @param player2 玩家2
     */
    public abstract void showMap(Player player1, Player player2);

    /**
     * 显示地图关卡对应的图形
     * @param value 地图当前位置的关卡代号,即图例符号的代号
     * @param index 当前地图位置编号
     * @param player1 玩家1
     * @param player2 玩家2
     * @return 地图当前位置的对应图片
     */
    public String getGraph(int value, int index, Player player1, Player player2){
        String graph = "";
        //设置给定的地图位置index的正确的图形符号graph
        if (index == 0) return icons.get("start");
        if (index != player1.getPlayerPos() && index != player2.getPlayerPos()) {
            graph += getPic(indexToIconName.get(value)); // 该位置没有玩家
        }
        else {
            if (player1.getPlayerPos() == player2.getPlayerPos()) {
                graph += player1.getPlayerSemiIcon() + player2.getPlayerSemiIcon();
            }
            else if (player1.getPlayerPos() == index) {
                graph += player1.getPlayerFullIcon();
            }
            else if (player2.getPlayerPos() == index) {
                graph += player2.getPlayerFullIcon();
            }
        }
        return graph;
    }

    public String getPic(String type) {
        return icons.get(type);
    }

    /**
     * 打印玩家的图标
     * @param player1 玩家1
     * @param player2 玩家2
     */
    public void showPlayerIcon(Player player1, Player player2) {
        System.out.println("^_^" + player1.getPlayerID() + "的士兵(" +
                player1.getPlayerName() + ")："+ player1.getPlayerSemiIcon());
        System.out.println("^_^" + player2.getPlayerID() + "的士兵(" +
                player2.getPlayerName() + ")："+ player2.getPlayerSemiIcon());
    }

    /**
     *
     */
    public void printIconInfo() {
        System.out.printf("图例说明：%s 起点; %s 终点; %s 暂停; %s 幸运轮盘; " +
                        "%s 地雷; %s 时空隧道; %s 普通;\n地图如下：\n",
                icons.get("start"), icons.get("end"), icons.get("pause"),
                icons.get("luckyTurn"), icons.get("landMine"),
                icons.get("timeTunnel"), icons.get("normal"));
    }
}
